<!-- 发病记录 -->
<template>
    <!-- 筛选条件 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="企业名称" v-if="companySelect">
            <el-select filterable clearable v-model="formInline.companyId">
                <el-option v-for="item in companyNameList" :label="item.name" :value="item.id" :key="item.id" />
            </el-select>
        </el-form-item>

        <el-form-item label="选择牛号">
            <el-select clearable filterable v-model="formInline.cowId">
                <el-option v-for="item in allComNameList" :label="`${item.code}-${item.breed}-${item.genderName}`" :value="item.id" />
            </el-select>
        </el-form-item>

        <el-form-item label="疾病">
            <el-select clearable filterable v-model="formInline.diseaseId">
                <el-option v-for="item in diseaseData" :key="item.value" :label="item.name" :value="item.value" />
            </el-select>
        </el-form-item>

        <el-form-item>
            <el-button icon="Search" @click="search">查询</el-button>
            <el-button type="success" icon="Download">导出</el-button>
        </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" stripe border style="width: 100%;">
        <el-table-column prop="recordPeople" label="牛号" width="350" fixed="left">
            <template #default="scope">
                <div style="display: flex;">
                    <span>{{ scope.row.cowCode }}</span>
                    <!-- 性别 -->
                    <el-tag v-if="scope.row.genderName == '公牛'" style="margin-left: auto;" effect="dark" type="primary" size="small">{{ scope.row.genderName }}</el-tag>
                    <el-tag v-if="scope.row.genderName == '母牛'" style="margin-left: auto;" effect="dark" type="danger" size="small">{{ scope.row.genderName }}</el-tag>
                    <!-- 品种 -->
                    <el-tag style="margin: 0 5px;" effect="dark" type="success" size="small">{{ scope.row.breed }}</el-tag>
                    <!-- 年龄 -->
                    <el-tag effect="dark" type="info" size="small">{{ scope.row.age }}</el-tag>
                </div>
            </template>
        </el-table-column>
        <el-table-column v-if="companyColumn" prop="companyName" label="企业名称" width="150" show-overflow-tooltip />
        <el-table-column prop="treatmentStatusName" label="健康状态" width="120">
            <template #default="scope">
                <span style="color: #2aae67;" v-if="scope.row.treatmentStatusName == '健康'">{{ scope.row.treatmentStatusName }}</span>
                <span style="color: #409eff;" v-else-if="scope.row.treatmentStatusName == '治愈'">{{ scope.row.treatmentStatusName }}</span>
                <span style="color: red;" v-else-if="scope.row.treatmentStatusName == '治疗中'">{{ scope.row.treatmentStatusName }}</span>
                <span v-else>{{ scope.row.treatmentStatusName }}</span>
            </template>
        </el-table-column>
        <el-table-column prop="birthday" label="出生日期" width="120" />
        <el-table-column prop="diseaseDate" label="发病日期" width="120" />
        <el-table-column prop="diseaseNames" label="疾病名称" width="250">
            <template #default="scope">
                <el-tag type="primary" style="margin-left: 5px;" v-for="(item, index) in scope.row.diseaseNames" :key="index">{{ item }}</el-tag>
            </template>
        </el-table-column>
        <el-table-column prop="treatmentPlan" label="治疗方案" width="200" />
        <el-table-column prop="treatmentStartDate" label="治疗开始日期" width="120" />
        <el-table-column prop="treatmentEndDate" label="治疗结束日期" width="120" />
        <el-table-column prop="treatmentDays" label="治疗天数" width="120" />
        <el-table-column prop="createUserName" label="记录人" width="100" />
        <el-table-column prop="createTime" label="记录日期" width="120" />
        <el-table-column label="操作" fixed="right" :width="tdWidthItem">
            <template #default="scope">
                <div ref="tdWidth" style="display: inline-block; white-space: normal;">
                <!-- <el-button type="success" icon="Edit" size="small">编辑</el-button> -->
                <el-button v-if="deleteButton" type="danger" icon="Delete" size="small">删除</el-button>
                <el-button v-if="errorButton" type="info" icon="Warning" size="small" @click="abnormalHandler(scope.row.id)">数据异常</el-button>
                </div>
            </template>
        </el-table-column>
    </el-table>
    <br/>
    <!-- 分页器 -->
    <el-pagination
        v-model:currentPage="pageNo"
        v-model:page-size="limit"
        :page-sizes="[15, 30, 50, 100]"
        :small="false"
        :background="true"
        layout="prev, pager, next, jumper, ->, sizes, total"
        :total="total"
        @current-change="getCowDiseasePage"
        @size-change="sizeChange"
    />
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { reqGetCompanyNameList } from '@/api/company'
import { reqGetDiseaseList } from '@/api/cattleStructure'
import { reqGetCowDiseasePage } from '@/api/daily'
import { reqCowDiseaseCowSelect } from '@/api/publicCowSelect'
import { reqCowDiseaseError } from '@/api/publicAbnormalInfo'
import { ElMessageBox, ElMessage } from 'element-plus'
// 引入用户相关小仓库
import useUserStore from '@/store/modules/user'
let userStore = useUserStore()
const companyColumn = userStore.componentConfig.companyColumn // 企业名称列-是否显示
const companySelect = userStore.componentConfig.companySelect // 企业下拉-是否显示
const addButton = userStore.componentConfig.addButton // 新增按钮-是否显示
const deleteButton = userStore.componentConfig.deleteButton // 删除按钮-是否显示
const editButton = userStore.componentConfig.editButton // 编辑按钮-是否显示
const errorButton = userStore.componentConfig.errorButton // 数据异常按钮-是否显示
const uploadButton = userStore.componentConfig.uploadButton // 导入按钮-是否显示

// 分页器当前页码
let pageNo = ref(1)
// 每一页展示多少条数据
let limit = ref(15)
// 数据总条数
let total = ref(0)

// 点击查询按钮
const search = () => {
    pageNo.value = 1
    getCowDiseasePage()
}

// 表单数据收集
const formInline = reactive({
    companyId: '', // 企业id
    cowId: '', // 牛id
    diseaseId: '', // 疾病id
    sortName: '', // 排序字段名称
    sortType: '', // 排序类型：ascending-正序、descending-倒序
})

// 获取牛疾病，下拉
let diseaseData = ref(null)
const getDiseaseList = async () => {
    let res = await reqGetDiseaseList()
    diseaseData.value = res.data
}

// 获取表格数据
const tableData = ref([])
const getCowDiseasePage = async () => {
    let res = await reqGetCowDiseasePage(Object.assign(formInline, {
        pageNumber: pageNo.value, // 页号
        pageSize: limit.value // 每页数量
    }))
    if(res.status == 200){
        total.value = res.data.totalRow
        tableData.value = res.data.records
    }
}


// 获取牛号下拉列表
let allComNameList = ref([])
const cowDiseaseCowSelect = async () => {
    let res = await reqCowDiseaseCowSelect()
    allComNameList.value = res.data
}

// 获取企业下拉列表数据
let companyNameList = ref([])
const getCompanyNameList = async () => {
    let res = await reqGetCompanyNameList()
    companyNameList.value = res.data
}

// 异常按钮
const abnormalHandler = (id) => {
    ElMessageBox.prompt('请输入异常原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal: false,
        inputPattern: /^.+$/,
        inputErrorMessage: '请填写异常原因'
    }).then(async (res) => {
        // console.log('异常原因为：', res.value)
        let result = await reqCowDiseaseError({
            id: id,
            auditMemo: res.value, // 异常信息
            status: -1
        })
        if(result.status == 200){
            ElMessage({
                type: 'success',
                message: result.msg
            })
        }
    }).catch(() => {
        console.log('取消填写异常原因')
    })
}

// 组件挂载完毕
onMounted(() => {
    cowDiseaseCowSelect()
    getCompanyNameList()
    getDiseaseList()
    getCowDiseasePage()
    widthAuto()
})

// 分页的下拉菜单发生改变时触发
const sizeChange = () => {
    pageNo.value = 1
    getCowSlaughterPage()
}

// 设置操作栏按钮，宽度自适应
const tdWidthItem = ref(200)
const tdWidth = ref()
const widthAuto = () => {
    setTimeout(() => {
        tdWidthItem.value = tdWidth.value.offsetWidth + 30
    }, 500)
}
</script>

<style scoped lang="scss">
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>